// buy/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: {},
    cinemaExtendList: [],
    showCinemas: [],
    showTimeList: [],
    cinemaList: [],
    activeTime: ""
  },
  changecinemaList(evt) {
    let time = evt.currentTarget.dataset.time;
    this.setData({ cinemaList: [], activeTime: time })
    let cinemas = this.data.showTimeList.filter(item => item.time == time)[0]["cinemaList"];
    wx.request({
      url: 'https://m.maizuo.com/gateway?k=1253227',
      method: "post",
      header: { "X-Host": "mall.film-ticket.cinema.batch-cinema" },
      data: { "cityId": 310100, "cinemaIds": cinemas.join(",") },
      success: ({ data }) => {
        this.setData({
          cinemaList: data.data.cinemas.map((item) => {
            item.lowprice = this.data.cinemaExtendList.filter(info => info.cinemaId == item.cinemaId)[0]["lowPrice"]
            return item
          })
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const id = options.id
    wx.request({
      url: `https://m.maizuo.com/gateway?filmId=${id}&k=3224642`,
      header: {
        "X-Host": "mall.film-ticket.film.info"
      },
      success: ({ data }) => {
        // console.log(data.data.film)
        this.setData({
          data: data.data.film
        })
      }
    })
    wx.request({
      url: `https://m.maizuo.com/gateway/?filmId=${id}&cityId=110100&k=3090154`,
      header: {
        "X-Host": "mall.film-ticket.cinema.film-show-cinema"
      },
      success: ({ data }) => {
        let showCinemasList = data.data.showCinemas.sort((a, b) => a.showDate - b.showDate);
        let weekList = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
        let templateDate = new Date();
        let currentDate = templateDate.getMonth() + 1 + "-" + templateDate.getDate();
        let templateDate2 = new Date();
        templateDate2.setDate(templateDate.getDate() + 1);
        let date2 = templateDate2.getMonth() + 1 + "-" + (templateDate2.getDate());
        let templateDate3 = new Date();
        templateDate3.setDate(templateDate.getDate() + 2);
        let date3 = templateDate3.getMonth() + 1 + "-" + (templateDate3.getDate());
        const showTimeList = showCinemasList.map(item => { //数据转换
          let date = new Date(item.showDate * 1000); //转成现在的时间
          let month = date.getMonth() + 1; //获取月份
          let day = date.getDate(); //获取天
          let week = date.getDay(); //获取星期几
          let str = "";
          if (month + "-" + day == currentDate) {
            str = "今天" + month + "月" + day + "日"
          } else if (month + "-" + day == date2) {
            str = "明天" + month + "月" + day + "日"
          } else if (month + "-" + day == date3) {
            str = "后天" + month + "月" + day + "日"
          } else {
            str = weekList[week] + month + "月" + day + "日";
          }
          return {
            cinemaList: item.cinemaList,
            time: str
          }
        })
        const cinemaExtendList = data.data.cinemaExtendList
        console.log(cinemaExtendList)

        this.setData({
          cinemaExtendList,
          showTimeList,
          activeTime: showTimeList[0].time
        })

        wx.request({
          url: 'https://m.maizuo.com/gateway?k=1253227',
          method: "post",
          header: {
            "X-Host": "mall.film-ticket.cinema.batch-cinema"
          },
          data: {
            "cityId": 310100,
            "cinemaIds": data.data.showCinemas.sort((a, b) => a.showDate - b.showDate)[0]["cinemaList"].join(",")
          },
          success: ({ data }) => {
            console.log(data.data.cinemas)
            this.setData({
              cinemaList: data.data.cinemas.map((item) => {
                item.lowprice = cinemaExtendList.filter(info => info.cinemaId == item.cinemaId)[0]["lowPrice"]
                return item
              })
            })
          }
        })



      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})